package com.lance.answer.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Objects;

public class HttpUtil {

    public static String postJson(String url, JSONObject json, String cookie, Map<String, String> heads) throws IOException {

        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个RequestBody(参数1：数据类型 参数2传递的json串)
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> entry : heads.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder.addHeader("accept", "application/json, text/plain, */*");
        builder.addHeader("Cookie", cookie);
        builder.addHeader("method", "POST");

        Request request = builder.url(url).post(requestBody).build();

        Response response = okHttpClient.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        return Objects.requireNonNull(response.body()).string();

    }


    public static String post(String url, Map<String, Object> data, String cookie, Map<String, String> heads) throws IOException {

        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个RequestBody(参数1：数据类型 参数2传递的json串)

        FormBody.Builder formBuilder = new FormBody.Builder();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            formBuilder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }

        System.out.println("cookie:" + cookie);
        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> entry : heads.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder.addHeader("accept", "application/json, text/plain, */*");
        builder.addHeader("Cookie", cookie);
        builder.addHeader("method", "POST");

        Request request = builder.url(url).post(formBuilder.build()).build();

        Response response = okHttpClient.newCall(request).execute();
        // if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        return Objects.requireNonNull(response.body()).string();

    }


    public static String get(String url, String cookie, Map<String, String> heads) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> entry : heads.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder.addHeader("accept", "application/json, text/plain, */*");
        builder.addHeader("Cookie", cookie);
        builder.addHeader("method", "GET");

        Request request = builder.url(url).get().build();

        Response response = okHttpClient.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        return Objects.requireNonNull(response.body()).string();
    }

    public static BufferedImage downloadImage(String imgUrl) {
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            DataInputStream in = new DataInputStream(connection.getInputStream());
            ImageInputStream is = ImageIO.createImageInputStream(in);
            BufferedImage bufferedImage = ImageIO.read(is);
            connection.disconnect();
            in.close();
            return bufferedImage;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public static String getLmsOuchn(String url, String cookie, Map<String, String> heads) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> entry : heads.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
//        builder.addHeader("accept", "application/json, text/plain, */*");
        builder.addHeader("Cookie", cookie);
//        builder.addHeader("method", "GET");

        Request request = builder.url(url).get().build();

        Response response = okHttpClient.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        return Objects.requireNonNull(response.body()).string();
    }
}
